package com.gamesite.gamesite.core.service.impl;

import com.gamesite.gamesite.core.pojo.GameRoom;
import com.gamesite.gamesite.core.pojo.User;
import com.gamesite.gamesite.core.service.GameRoomService;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class GameRoomServiceImpl implements GameRoomService {
    private final Map<Long, GameRoom> rooms = new ConcurrentHashMap<>();

    // 创建房间
    public GameRoom createRoom(Long roomId) {
        GameRoom room = new GameRoom();
        room.setRoomId(roomId);
        rooms.put(roomId, room);
        return room;
    }

    // 获取房间
    public GameRoom getRoom(Long roomId) {
        return rooms.get(roomId);
    }

    // 加入房间
    public boolean joinRoom(Long roomId, User player) {
        GameRoom room = rooms.get(roomId);
        if (room != null && !room.getIsStart()) {
            return true; // 返回boolean表示是否成功加入
        }
        return false;
    }

    // 开始游戏
    public void startGame(Long roomId) {
        GameRoom room = rooms.get(roomId);
        if (room != null && room.getPlayerList().size() == 2) {
            room.setIsStart(true);
        }
    }
}